package com.yhf.controller;

import com.yhf.bean.Admin;
import com.yhf.bean.LoginForm;
import com.yhf.bean.Student;
import com.yhf.bean.Teacher;
import com.yhf.service.AdminService;
import com.yhf.service.StudentService;
import com.yhf.service.TeacherService;
import com.yhf.utils.CreateVerifiCodeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.management.Attribute;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/system")
public class SystemController {
    private Map<String,Object> result = new HashMap<String,Object>();
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @GetMapping("getVerifiCodeImage")
    public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response){
        BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
        String verifiCode = String.valueOf(CreateVerifiCodeImage.getVerifiCode());
        try {
            ImageIO.write(verifiCodeImage,"JPEG",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();

        }
        //存入session
        request.getSession().setAttribute("verifiCode", verifiCode);
    }
    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(LoginForm loginForm, HttpServletRequest request){
        String verifiCode = (String) request.getSession().getAttribute("verifiCode");
        if("".equals(verifiCode)){
            result.put("success",false);
            result.put("msg","长时间未操作刷新重试");
            return result;
        }else if(!verifiCode.equalsIgnoreCase(loginForm.getVerifiCode())){
            result.put("success",false);
            result.put("msg","验证码输入错误");
            return result;
        }
        request.getSession().removeAttribute("verifiCode");
        switch (loginForm.getUserType()){
            case "1":
                try {
                    Admin admin = adminService.login(loginForm);
                    if (admin!=null){
                        request.getSession().setAttribute("userInfo",admin);
                        request.getSession().setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success",false);
                    result.put("msg","登录失败");
                    return result;
                }

                break;
            case "2":
                try {
                    Student student = studentService.login(loginForm);
                    if (student!=null){
                        request.getSession().setAttribute("userInfo",student);
                        request.getSession().setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success",false);
                    result.put("msg","登录失败");
                    return result;
                }



                break;
            case "3":
                try {
                    Teacher teacher = teacherService.login(loginForm);
                    if (teacher!=null){
                        request.getSession().setAttribute("userInfo",teacher);
                        request.getSession().setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success",false);
                    result.put("msg","登录失败");
                    return result;
                }



                break;

        }
        result.put("success",false);
        result.put("msg","账号密码错误");
        return result;
    }
    @GetMapping("goSystemMainView")
    public String goSystemMainView(){
        return "system/main";
    }
    @GetMapping("loginOut")
public String loginOut(HttpServletRequest request){
        request.getSession().removeAttribute("userInfo");
        request.getSession().removeAttribute("userType");
        return "system/login";
}

}
